<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" style="height: 45px">
            <a-form-item>
              <a @click="">
                ┃ 工单信息
              </a>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input v-model="model.orderNo" placeholder="请输入工单号"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seqNo">
              <a-input-number v-model="model.seqNo" placeholder="请输入序号" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工序编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processNo">
              <a-input v-model="model.processNo" placeholder="请输入工序编号"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工序名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processName">
              <a-input v-model="model.processName" placeholder="请输入工序名称"  disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工作中心" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workId">
              <j-dict-select-tag type="list" v-model="model.workId" dictCode="mdm_work_center,name,id" placeholder="" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="产品编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialNo">
              <a-input v-model="model.materialNo" placeholder="请输入产品编号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="产品描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="materialDesc">
              <a-input v-model="model.materialDesc" placeholder="请输入产品描述"disabled  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <j-dict-select-tag type="list" v-model="model.unit" dictCode="mdm_unit,unit_name,unit_code" placeholder="请选择单位" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工单数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderQty">
              <a-input-number v-model="model.orderQty" placeholder="请输入工单数量" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="加工数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qty">
              <a-input-number v-model="model.qty" placeholder="请输入加工数量" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="合格数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="completeQty">
              <a-input-number v-model="model.checkQty" placeholder="请输入合格数量" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="报废数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scrapQty">
              <a-input-number v-model="model.scrapQty" placeholder="请输入报废数量" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="返修数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reworkQty">
              <a-input-number v-model="model.reworkQty" placeholder="请输入返修数量" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" style="height: 45px">
            <a-form-item>
              <a @click="">
                ┃ 完工汇报
              </a>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="可汇报数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unhandleQty">
              <a-input-number v-model="model.unhandleQty" placeholder="" style="width: 100%" disabled=""/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="完工数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportQty">
              <a-input-number v-model="model.reportQty" placeholder="请输入完工数量" style="width: 100%" @change="reportQtyChanged"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="合格数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportCheckQty">
              <a-input-number v-model="model.reportCheckQty" placeholder="请输入合格数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="报废数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportScrapQty">
              <a-input-number v-model="model.reportScrapQty" placeholder="请输入报废数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="返修数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportReworkQty">
              <a-input-number v-model="model.reportReworkQty" placeholder="请输入返修数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="completeTime">
              <a-input-number v-model="model.completeTime" placeholder="请输入本次本次工时" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="汇报人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportUser">
              <yq-select-user-by-dep v-model="model.reportUser" text="realname,orgCodeTxt" :multi="false"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction, putAction} from '@/api/manage'
  import {isNumber, validateDuplicateValue} from '@/utils/util'
  import store from "@/store";

  export default {
    name: 'PpWorkOrderProcessReportForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          reportQty: 0,
          reportCheckQty: 0,
          reportScrapQty: 0,
          reportReworkQty: 0,
          completeTime: 0,
          reportUser: store.getters.userInfo.username,
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          handleReport: "/pp/workOrderProcess/handleReport",
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({},this.modelDefault, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            if(!this.model.reportQty > 0){
              this.$message.error("完工汇报数量必须大于0");
              return;
            }
            that.confirmLoading = true;
            let handleData = {
              id: that.model.id,
              reportQty: that.model.reportQty,
              reportCheckQty: that.model.reportCheckQty,
              reportScrapQty: that.model.reportScrapQty,
              reportReworkQty: that.model.reportReworkQty,
              completeTime: that.model.completeTime,
              reportUser: that.model.reportUser};
            putAction(this.url.handleReport, handleData).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      reportQtyChanged(value){
        if(!isNumber(value)){
          this.model.reportQty = 0;
        }
        this.model.reportCheckQty = this.model.reportQty;
      },
    }
  }
</script>
